package middleware

import (
	"reflect"

	validator "gopkg.in/go-playground/validator.v9"
)

type EchoValidator struct {
	validator *validator.Validate
}

func NewValidator() *EchoValidator {
	return &EchoValidator{
		validator: validator.New(),
	}
}

func (c *EchoValidator) Validate(i interface{}) error {
	val := reflect.Indirect(reflect.ValueOf(i))
	switch val.Kind() {
	case reflect.Slice:
		for j := 0; j < val.Len(); j++ {
			err := c.validator.Struct(val.Index(j).Interface())
			if err != nil {
				return err
			}
		}
		return nil
	}
	return c.validator.Struct(i)
}
